package com.zoweunion.mechanic.service.base.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.zoweunion.mechanic.entity.CarTemplateBean;
import com.zoweunion.mechanic.entity.PartsTemplateBean;
import com.zoweunion.mechanic.model.ResponseBean;
import com.zoweunion.mechanic.model.User;
import com.zoweunion.mechanic.service.base.BaseService;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.Page;
import com.zoweunion.mechanic.util.constants.Constants;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zoweunion.mechanic.dao.base.PartsDao;
import com.zoweunion.mechanic.dao.base.RoleDao;
import com.zoweunion.mechanic.service.base.PartsService;
import com.zoweunion.mechanic.service.base.RoleService;
import com.zoweunion.mechanic.util.PageUtil;
import com.zoweunion.mechanic.util.UuidUtil;
/**
 * 配件业务类
 * @author Administrator
 *
 */
@Service
@Transactional()
public class PartsServiceImpl extends BaseService implements PartsService {

	@Autowired
	private PartsDao partsDao;
	
	@Override
	@Transactional(readOnly=true)
	public Map<String, Object> getPageQuery(Map<String, Object> reqMap) {
		//1.设置分页的limit和offset
		Map<String, Object> pageMap = this.appPage(reqMap);
		int limit = Integer.valueOf(pageMap.get("limit").toString());
		//2.查询总记录数
		int total = partsDao.countBycondition(reqMap);
		
		//3执行分页查询
		List<Map<String, Object>> list = partsDao.pageQuery(reqMap);
		
		//4包装结果集  总条数 总页数 数据集
		HashMap<String, Object> resultMap = new HashMap<>();
		
		//int pages=(total+limit-1)/limit;//总页数  需要有余进一
		
		int pages = total%limit == 0 ? total/limit : (total/limit)+1;
		
		resultMap.put("total", total);
		resultMap.put("pages", pages);
		resultMap.put("list", list);
		return resultMap;
	}

	@Override
	public void insert(Map<String, Object> reqMap) {
		reqMap.put("id",UuidUtil.get32UUID());
		partsDao.insert(reqMap);
	}


	@Override
	@Transactional(readOnly=true)
	public int checkExistByDictionaries_code(Map<String, Object> reqMap) {
		return partsDao.countByDictionaries_code(reqMap);
	}

	@Override
	public void update(Map<String, Object> reqMap) {
		partsDao.update(reqMap);
	}

	@Override
	public Map<String, Object> getDictionariesList(Map<String, Object> reqMap) {
		Map<String,Object> map=new HashMap<>();
		map.put("name",partsDao.getDictionariesNameList(reqMap));
		map.put("type",partsDao.getDictionariesTypeList(reqMap));
		map.put("format",partsDao.getDictionariesFormatList(reqMap));
		return map;
	}

	@Override
	public List<String> getParameterList(Map<String, Object> reqMap){
		return partsDao.getParameterList(reqMap);
	}

	@Override
	public void uploadParts(List<PartsTemplateBean> result, User currentUser)throws MyException {
		Map<String, Object> hashMap=null;
		for (PartsTemplateBean item : result) {
			if(StringUtils.isBlank(item.getPartsName())){
				throw new MyException(Constants.CODE_HANDEL_FAILED,"配件名称为空,请填写配件名称!");
			}
			if(StringUtils.isBlank(item.getPartsType())){
				throw new MyException(Constants.CODE_HANDEL_FAILED,"配件类型为空,请填写配件类型!");
			}
			if(StringUtils.isBlank(item.getPartsCode())){
				throw new MyException(Constants.CODE_HANDEL_FAILED,"配件编号为空,请填写配件编号!");
			}
			if(StringUtils.isBlank(item.getPartsPrice())){
				throw new MyException(Constants.CODE_HANDEL_FAILED,"配件价格为空,请填写配件价格!");
			}
			if(StringUtils.isBlank(item.getPartsUnit())){
				throw new MyException(Constants.CODE_HANDEL_FAILED,"配件单位为空,请填写配件单位!");
			}
			hashMap = new HashMap<>();
			hashMap.put("s_id",currentUser.getS_id());

			hashMap.put("dictionaries_name",item.getPartsName());
			/**
			 * 1: 原厂
			 * 2:副厂
			 */
			int type=0;
			if(item.getPartsType().equals("原厂")){
				type=1;
			}else if(item.getPartsType().equals("副厂")){
				type=2;
			}else{
				throw new MyException(Constants.CODE_HANDEL_FAILED,"配件类型输入错误,请填写配件类型!");
			}

			hashMap.put("dictionaries_type",type);
			hashMap.put("dictionaries_code",item.getPartsCode());
			hashMap.put("dictionaries_unit",item.getPartsUnit());
			hashMap.put("dictionaries_model",item.getPartsModel());
			//插入前需要根据dictionaries_code校验是否已经存在
			int exist = this.checkExistByDictionaries_code(hashMap);
			if(exist>0) {
				throw new MyException(Constants.CODE_HANDEL_FAILED,"已存在该编号的配件");
			}
		}
		Map<String, Object> insertMap=null;
		for (PartsTemplateBean item : result) {
			insertMap = new HashMap<>();

			int type=0;
			if(item.getPartsType().equals("原厂")){
				type=1;
			}else if(item.getPartsType().equals("副厂")){
				type=2;
			}
			insertMap.put("dictionaries_name",item.getPartsName());
			insertMap.put("dictionaries_type",type);
			insertMap.put("dictionaries_code",item.getPartsCode());
			insertMap.put("dictionaries_unit",item.getPartsUnit());
			insertMap.put("dictionaries_model",item.getPartsModel());
			insertMap.put("dictionaries_price",item.getPartsPrice());
			insertMap.put("dictionaries_spec",item.getPartsSpec());
			insertMap.put("dictionaries_format",item.getPartsFormat());
			insertMap.put("remark",item.getPartsRemark());
			insertMap.put("s_id",currentUser.getS_id());
			insertMap.put("update_user",currentUser.getId());
			insertMap.put("create_user",currentUser.getId());
			this.insert(insertMap);
		}
	}
}
